package cn.tedu.csmall.stock.webapi.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //使用常量定义交换机和队列、路由键的变量名以及值
    public static final String STOCK_EXCHANGE="stock_ex";
    public static final String STOCK_QUEUE="stock_queue";
    public static final String STOCK_ROUTE="stock_route";

    //创建交换机，保存到Spring容器中
    @Bean
    public DirectExchange stockDirectExchange(){
        return new DirectExchange(STOCK_EXCHANGE);
    }
    //创建队列。需要交给Spring容器管理
    @Bean
    public Queue stockQueue(){
        return new Queue(STOCK_QUEUE);
    }
    //声明路由键，绑定队列和交换机，把绑定关系也需要交给Spring管理
    @Bean
    public Binding stockBinding(){
        return BindingBuilder.bind(stockQueue()).to(stockDirectExchange()).with(STOCK_ROUTE);
    }
}
